<?php
require_once('ezsqlcore.class.php');
$ezsql_mysql_str = array
(
	1 => 'Require $dbuser and $dbpassword to connect to a database server',
	2 => 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?',
	3 => 'Require $dbname to select a database',
	4 => 'mySQL database connection is not active',
	5 => 'Unexpected error while trying to select database'
);

if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');

class ezSQLMysql extends ezSQLcore
{

	var $dbuser = false;
	var $dbpassword = false;
	var $dbname = false;
	var $dbhost = false;
	private static $instance=array();

	public function __construct($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
	{
		$this->dbuser = $dbuser;
		$this->dbpassword = $dbpassword;
		$this->dbname = $dbname;
		$this->dbhost = $dbhost;
		$this->query('SET NAMES UTF8');
	}

	static function getInstance($key)
	{
		if(!isset(self::$instance[$key]))
		{
			global $config;
			extract($config[$key]);
			self::$instance[$key] = new ezSQLMysql($user,$password,$dbname,$host); 	
		}
		return self::$instance[$key];
	}

	public function quickConnect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
	{
		$return_val = false;
		if ( ! $this->connect($dbuser, $dbpassword, $dbhost,true) ) ;
		else if ( ! $this->select($dbname) ) ;
		else $return_val = true;
		return $return_val;
	}

	public function connect($dbuser='', $dbpassword='', $dbhost='localhost')
	{
		global $ezsql_mysql_str; $return_val = false;
		if ( ! $dbuser )
		{
			$this->registerError($ezsql_mysql_str[1].' in '.__FILE__.' on line '.__LINE__);
			$this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
		}
		else if ( ! $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword,true,131074) )
		{
			$this->registerError($ezsql_mysql_str[2].' in '.__FILE__.' on line '.__LINE__);
			$this->show_errors ? trigger_error($ezsql_mysql_str[2],E_USER_WARNING) : null;
		}
		else
		{
			$this->dbuser = $dbuser;
			$this->dbpassword = $dbpassword;
			$this->dbhost = $dbhost;
			$return_val = true;
		}

		return $return_val;
	}

	public function select($dbname='')
	{
		global $ezsql_mysql_str; $return_val = false;

		if ( ! $dbname )
		{
			$this->registerError($ezsql_mysql_str[3].' in '.__FILE__.' on line '.__LINE__);
			$this->show_errors ? trigger_error($ezsql_mysql_str[3],E_USER_WARNING) : null;
		}
		else if ( ! $this->dbh )
		{
			$this->registerError($ezsql_mysql_str[4].' in '.__FILE__.' on line '.__LINE__);
			$this->show_errors ? trigger_error($ezsql_mysql_str[4],E_USER_WARNING) : null;
		}
		else if ( !@mysql_select_db($dbname,$this->dbh) )
		{
			if ( !$str = @mysql_error($this->dbh))
			{
				$str = $ezsql_mysql_str[5];
			}
			$this->registerError($str.' in '.__FILE__.' on line '.__LINE__);
			$this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
		}
		else
		{
			$this->dbname = $dbname;
			$return_val = true;
		}

		return $return_val;
	}

	public function escape($str)
	{
		if ( ! isset($this->dbh) || ! $this->dbh )
		{
			$this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
			$this->select($this->dbname);
		}
		return mysql_real_escape_string(stripslashes($str));
	}

	public function sysdate()
	{
		return 'NOW()';
	}

	public function query($query)
	{
		// Initialise return
		$return_val = 0;
		$this->flush();
		$query = trim($query);
		$this->func_call = "\$db->query(\"$query\")";
		$this->last_query = $query;
		$this->num_queries++;
		$this->sqlLog($query);
		if ( $cache = $this->getCache($query) )
		{
			return $cache;
		}

		if ( ! isset($this->dbh) || ! $this->dbh )
		{
			$this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
			$this->select($this->dbname);
		}

		$this->result = @mysql_query($query,$this->dbh);

		// If there is an error then take note of it..
		if ( $str = @mysql_error($this->dbh) )
		{
			$this->sqlLog($query,'error');
			$is_insert = true;
			$this->registerError($str);
			$this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
			return false;
		}

		$is_insert = false;
		if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
		{
			$this->rows_affected = @mysql_affected_rows($this->dbh);
			if ( preg_match("/^(insert|replace)\s+/i",$query) )
			{
				$this->insert_id = @mysql_insert_id($this->dbh);
			}
			$return_val = $this->rows_affected;
		}
		else
		{

			$i=0;
			while ($i < @mysql_num_fields($this->result))
			{
				$this->col_info[$i] = @mysql_fetch_field($this->result);
				$i++;
			}

			$num_rows=0;
			while ( $row = @mysql_fetch_assoc($this->result) )
			{
				$this->last_result[$num_rows] = $row;
				$num_rows++;
			}
			@mysql_free_result($this->result);
			$this->num_rows = $num_rows;
			$return_val = $this->num_rows;
		}
		$this->storeCache($query,$is_insert);
		$this->trace || $this->debug_all ? $this->debug() : null ;

		return $return_val;
	}
}
